var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);
// 获取商品一级分类
router.get('/jkyx/pc/goods_classify', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_GoodsClassify,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			classify: results
	     		}));	         
			}
		})
	})
});
// 获取商品二级分类
router.get('/jkyx/pc/goods_classify2', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_GoodsClassify2,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			classify: results
	     		}));	         
			}
		})
	})
});
// 获取全部分组
router.get('/jkyx/pc/goods_all_classify', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_AllClassify,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				// 全部1级分类
				var clss1 = results[0];
				// 全部2级分类
				var clss2 = results[1];
				var len1 = clss1.length;
				var len2 = clss2.length;
				var data =[];
				for (var i = 0; i < len1; i ++) {
					var item = {};
					item.id = clss1[i].id;
					item.par_id = clss1[i].pid;
					item.label = clss1[i].name;
					item.desc = clss1[i].desc;
					item.logo = clss1[i].logo;
					item.sort = clss1[i].sort;
					item.show_status = clss1[i].show_status;
					item.children = [];
					for (var j = 0; j < len2; j++) {
						if(clss1[i].id == clss2[j].pid){
							var sub = {};
							sub.id = clss2[j].id;
							sub.par_id = clss2[j].pid;
							sub.label = clss2[j].name;
							sub.desc = clss2[j].desc;
							sub.logo = clss2[j].logo;
							sub.sort = clss2[j].sort;
							sub.show_status = clss2[j].show_status;
							item.children.push(sub);
						}
					}
					data.push(item);
				}
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			classify: data
	     		}));	         
			}
		})
	})
});
// 获取某个分组下的商品
router.get('/jkyx/pc/goods_by_classify', function(req, res, next) {
	var param = req.query || req.params; 
	var classify_id = param.classify_id || '';
	if (classify_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入分类id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_GoodsByClassify,[classify_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var len = results.length;
				for (var i = 0; i < len; i ++) {
					results[i].imgs = JSON.parse(results[i].imgs);
				}
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: results
	     		}));	         
			}
		})
	})
});

// 删除某个分组
router.get('/jkyx/pc/delete_classify', function(req, res, next) {
	var param = req.query || req.params; 
	var classify_id = param.classify_id || '';
	if (classify_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入分类id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_deletc_classify,[classify_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100'
	     		}));	         
			}
		})
	})
});
// 保存某个分组
router.get('/jkyx/pc/save_classify', function(req, res, next) {
	var param = req.query || req.params; 
	var id = param.id || '';
	var pid = param.pid || '';
	var name = param.name || '';
	var desc = param.desc || '';
	var logo = param.logo || '';
	var sort = param.sort || '';
	var show_status = param.show_status || 1;
	var update_time = CommonJS.getNowFormatDate();
	if (id == '') {
		res.end(JSON.stringify({
 			msg: '请传入分类id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_save_classify,[pid,name,desc,logo,sort,show_status,update_time,id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100'
	     		}));	         
			}
		})
	})
});
// 新建分组
router.get('/jkyx/pc/new_classify', function(req, res, next) {
	var param = req.query || req.params; 
	var level = param.level || 1;
	var pid = param.pid || 0;
	var name = param.name || '';
	var desc = param.desc || '';
	var logo = param.logo || '';
	var sort = param.sort || '';
	var show_status = param.show_status || 1;
	var update_time = CommonJS.getNowFormatDate();
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		                                         //level,pid,name,`desc`,logo,sort,create_time,update_time
		connection.query(DBConfig.pc_new_classify,[level,pid,name,desc,logo,sort,update_time,update_time,show_status],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100'
	     		}));	         
			}
		})
	})
});


module.exports = router;